<template>
  <div class="mod-bus__busnotice">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.title" placeholder="标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <ren-select v-model="state.dataForm.textType" dict-type="notice_text_type" placeholder="内容类型"
                    clearable></ren-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="state.dataForm.isPublish" placeholder="是否发布" clearable>
          <el-option label="否" value="0"></el-option>
          <el-option label="是" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="state.dataForm.isTopped" placeholder="是否置顶" clearable>
          <el-option label="否" value="0"></el-option>
          <el-option label="是" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('bus:busnotice:save')" type="primary" @click="addOrUpdateHandle()">{{
            $t("add")
          }}{{ moduleName }}
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('bus:busnotice:delete')" type="danger" @click="state.deleteHandle()">{{
            $t("deleteBatch")
          }}{{ moduleName }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
              @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="sort" label="排序" header-align="center" align="center" width="100"></el-table-column>
      <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
      <el-table-column prop="textType" label="内容类型" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("notice_text_type", scope.row.textType) }}
        </template>
      </el-table-column>
      <el-table-column prop="description" label="简述" header-align="center" align="center">
        <template v-slot="scope">
          <el-tooltip :content="scope.row.description" placement="top">
            <p class="showOverTooltip">{{ scope.row.description }}</p>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址" header-align="center" align="center">
        <template v-slot="scope">
          <el-tooltip :content="scope.row.address" placement="top">
            <p class="showOverTooltip">{{ scope.row.address }}</p>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="isPublish" label="是否发布" header-align="center" align="center">
        <template v-slot="scope">
          <el-switch :model-value="scope.row.isPublish" :active-value="1" :inactive-value="0"
                     @click="publishRow(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column prop="isTopped" label="是否置顶" header-align="center" align="center">
        <template v-slot="scope">
          <el-switch :model-value="scope.row.isTopped" :active-value="1" :inactive-value="0"
                     @click="topRow(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column prop="image" label="预览图片" header-align="center" align="center">
        <template v-slot="scope">
          <el-image v-if="scope.row.image"
                    :src=" 'https://' + scope.row.image"
                    style="height: 50px;width: 50px;"
                    :preview-src-list="['https://' +scope.row.image]"
                    :preview-teleported="true"
                    :initial-index="0"
                    fit="cover"
          ></el-image>
          <div v-else>无</div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('bus:busnotice:update')" type="primary" link
                     @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}
          </el-button>
          <el-button v-if="state.hasPermission('bus:busnotice:delete')" type="primary" link
                     @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {IObject} from "@/types/interface";
import baseService from "@/service/baseService";
import {reactive, ref, toRefs} from "vue";
import AddOrUpdate from "./busnotice-add-or-update.vue";

//子组件接收父组件传递过来的值
const props = defineProps(["moduleCode", "moduleName"]);
const moduleCode = ref(props.moduleCode);
const moduleName = ref(props.moduleName);

const view = reactive({
  getDataListURL: "/bus/busnotice/page",
  getDataListIsPage: true,
  exportURL: "/bus/busnotice/export",
  deleteURL: "/bus/busnotice",
  deleteIsBatch: true,
  dataForm: {
    type: moduleCode.value,
    title: "",
    textType: "",
    isPublish: "",
    isTopped: "",
  }
});

const state = reactive({...useView(view), ...toRefs(view)});

const addKey = ref(0);
const addOrUpdateRef = ref();
/*const addOrUpdateHandle = (id?: number) => {
    addKey.value++;
    nextTick(() => {
        addOrUpdateRef.value.init(id);
    });
};*/
const addOrUpdateHandle = (id?: number) => {
  let params = {
    id: id,
    type: moduleCode.value
  };
  addOrUpdateRef.value.init(params);
};

// 是否发布
const publishRow = (row: IObject) => {
  row.isPublish = row.isPublish ? 0 : 1
  baseService.put("/bus/busnotice/", row).then((res) => {
    state.getDataList();
  });
};

// 是否置顶
const topRow = (row: IObject) => {
  row.isTopped = row.isTopped ? 0 : 1
  baseService.put("/bus/busnotice/", row).then((res) => {
    state.getDataList();
  });
};

</script>

<style lang="less" scoped>
.showOverTooltip {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  /*这里是2行*/
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
